package org.xiaoyu.rpc.remoting.transport.server;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.timeout.IdleStateEvent;
import io.netty.handler.timeout.IdleStateHandler;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

/**
 * Created By Have
 * 2021/7/12 18:05
 */
@Slf4j
@ChannelHandler.Sharable
public class RpcIdleHandler extends IdleStateHandler {
    public static final RpcIdleHandler INSTANCE = new RpcIdleHandler();
    private static final int READ_IDLE_TIME = 10 * 60;

    public RpcIdleHandler() {
        super(READ_IDLE_TIME, READ_IDLE_TIME, READ_IDLE_TIME, TimeUnit.SECONDS);
    }

    @Override
    protected void channelIdle(ChannelHandlerContext ctx, IdleStateEvent evt) throws Exception {
        log.info("客户端id:[{}],Address:[{}],未在[{}]秒内发起请求,断开连接。", ctx.channel().id(), ctx.channel().remoteAddress(), READ_IDLE_TIME);
        ctx.channel().close();
    }
}
